/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import javax.annotation.PostConstruct;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.constants.ICommonConstants;
import com.lmx.persistence.service.PayRecordService;
import com.lmx.repoDomain.tmodel.PayRecord;

import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

@JsonApiResourceRepository(PayRecord.class)
@Component
public class PayRecordRepository extends BaseRepository<PayRecord>{
    private Log logger = LogFactory.getLog(this.getClass());
    @Autowired
    private PayRecordService payRecordService;

    @PostConstruct
    public void init() {
        dslJpaService.setEntityClass(PayRecord.class);
    }

/*    @Override
    @JsonApiFindAll
    public Iterable<PayRecord> findAll(final QueryParams requestParams) {
        logger.debug("pryrecord findAll  ----  return null");
        return null;
    }*/

    @Override
    @JsonApiSave
    public PayRecord save(PayRecord payRecord) {
        if (payRecord.getId() == null) {
            logger.debug("创建支付记录");
            payRecord.setPayStatus((short)ICommonConstants.PAYSTATUS.APPLY.ordinal());
            if(payRecord.getPayType() == ICommonConstants.PAYTYPE.WITHDRAW.ordinal()){
                logger.debug("开始提现申请验证");
                payRecord = payRecordService.Withdraw(payRecord);
            }else if(payRecord.getPayType() == ICommonConstants.PAYTYPE.RECHARGE.ordinal()){
                logger.debug("充值： 扫码支付");
//                payRecord = (PayRecord)dslJpaService.create(payRecord);
                payRecord = payRecordService.recharge(payRecord);

            }else {
                payRecord.setErrcode(ICommonConstants.INTERFACEERRO.NOINTERFACE.ordinal());
            }

        } else {
            logger.debug("禁止jsonAPI接口修改支付记录表");
//            dslJpaService.update(payRecord);
        }
        return payRecord;
    }


}
